Crate cached [−] [src]
A macro for defining cached/memoized functions that wrap a static-ref cache object.
Note: Due to the requirements of storing arguments and return values in a global cache,
function arguments and return types must be owned, function arguments must implement Hash + Eq + Clone
,
and function return types must implement Clone
.
Arguments and return values will be cloned in the process of insertion and retrieval.
cached!
functions should not be used to produce side-effectual results!
Usage & Options:
There's several options depending on how explicit you want to be. See below for full syntax breakdown.
1.) Use an explicitly specified cache-type and provide the instantiated cache struct.
For example, a SizedCache
(LRU).
#[macro_use] extern crate cached; #[macro_use] extern crate lazy_static; use cached::SizedCache; cached!{FIB: SizedCache = SizedCache::with_capacity(50); >> fn fib(n: u64) -> u64 = { if n == 0 || n == 1 { return n } fib(n-1) + fib(n-2) }}
2.) Use an explicitly specified cache-type, but let the macro instantiate it.
The cache-type is expected to have a new
method that takes no arguments.
#[macro_use] extern crate cached; #[macro_use] extern crate lazy_static; use cached::UnboundCache; cached!{FIB: UnboundCache >> fn fib(n: u64) -> u64 = { if n == 0 || n == 1 { return n } fib(n-1) + fib(n-2) }}
3.) Use the default unbounded cache.
#[macro_use] extern crate cached; #[macro_use] extern crate lazy_static; cached!{FIB >> fn fib(n: u64) -> u64 = { if n == 0 || n == 1 { return n } fib(n-1) + fib(n-2) }}
Cache Types
Several caches are available in this crate:
cached::UnboundCache
cached::SizedCache
cached::TimedCache
Any custom cache that implements cached::Cached
can be used in place of the built-ins.
Syntax:
The complete macro syntax is:
cached!{CACHE_NAME: CacheType = CacheType::constructor(arg); >> fn func_name(arg1: arg_type, arg2: arg_type) -> return_type = { // do stuff like normal return_type }}
Where:
CACHE_NAME
is the unique name used to hold astatic ref
to the cacheCacheType
is the struct type to use for the cache (Note, this cannot be namespaced, e.g.cached::SizedCache
will not be accepted by the macro.SizedCache
must be imported and passed directly)CacheType::constructor(arg)
is any expression that yields an instance ofCacheType
to be used as the cache-store, followed by; >>
fn func_name(arg1: arg_type) -> return_type
is the same form as a regular function signature, with the exception that functions with no return value must be explicitly stated (e.g.fn func_name(arg: arg_type) -> ()
)- The expression following
=
is the function body assigned tofunc_name
. Note, the function body can make recursive calls to its cached-self (func_name
).
Reexports
pub use stores::*; |
Modules
macros |
Macro for defining functions that wrap a static-ref cache object. |
stores |
Implementation of various caches |
Macros
cached |
Traits
Cached |
Cache operations |